﻿using Microsoft.Win32;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace WpfApp1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 将打开的txt内容读到T2这个TextBlock，或者说读文件到缓冲区
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "选择数据源文件";
            openFileDialog.Filter = "txt文件|*.txt|所有文件|*.*";
            openFileDialog.FileName = string.Empty;
            openFileDialog.FilterIndex = 1;
            openFileDialog.Multiselect = false;
            openFileDialog.RestoreDirectory = true;
            openFileDialog.DefaultExt = "txt";
            if (openFileDialog.ShowDialog() == false)
            {
                return;
            }

            string contents = System.IO.File.ReadAllText(openFileDialog.FileName, Encoding.Default);
            T2.Text = contents;
        }

        /// <summary>
        /// 统计每个单词的频率，存放在字典word_freq
        /// </summary>
        /// <param name="strword"></param>
        /// <returns>返回一个word_freq</returns>
        private Dictionary<string, int> process_Buffer(ArrayList strword) {

            Hashtable ht = new Hashtable(); 
            foreach (string i in strword)//以哈夫曼树记录统计词频，遍历wtrword
                if (!ht.Contains(i))
                    ht.Add(i, 1);
                else
                {
                    ht[i] = (int)ht[i] + 1;
                }

            Dictionary<string, int> word_freq = new Dictionary<string, int>();
            foreach (DictionaryEntry de in ht)//导入字典
            {
                word_freq.Add(de.Key.ToString(), Convert.ToInt32(de.Value));
            }

            word_freq = (from entry in word_freq orderby entry.Value ascending select entry).ToDictionary(pair => pair.Key, pair => pair.Value);
            return word_freq;
        }

        /// <summary>
        /// 疯狂切字符串
        /// </summary>
        /// <param name="strtext_low">一个已经变成小写的文本</param>
        /// <returns>返回一个已经被切好的ArrayList字符串列表</returns>
        private ArrayList separateword(string strtext_low)
        {
            ArrayList sepword = new ArrayList();//定义一个动态数组用于传进已转换小写的数组
            string strtext = strtext_low;
            strtext = strtext.Replace(',', ' ');//用Replace（）方法将‘，’，‘。’转化为空格
            strtext = strtext.Replace('.', ' ');
            string[] word = new string[500];//定义一个500长度的数组接收转换完成的单词
            word = strtext.Split(' ');//使用Split（）方法分割单词
            foreach (string i in word)//遍历循环将其传入动态数组sepword
                sepword.Add(i);
            return sepword;
        }


        /// <summary>
        /// 相当于最后的“if __name__ == "__main__":”的执行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            T1.Text = "";
            int i = 0;
            foreach (var item in process_Buffer(separateword(T2.Text.ToLower())).Reverse())
            {
                if (i > 10)
                {
                    break;
                }
                if (item.Key != "")
                {
                    string str = string.Format("[" + i + "]" + "{0,-15} {1,3}", item.Key, item.Value);
                    T1.Text = T1.Text + str + "\r\n";
                }
                i++;
            }
            i = 0;

        }

    }
}
